﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Bll;
using Runda.EShop.Common.Entity;
using Runda.EShop.Common.Utility;

namespace Runda.EShop.Web.Order
{
    public partial class OrderComplaintView : BasePage
    {
        public Guid ComplainID
        {
            get
            {
                if (Request.QueryString["ComplainID"] != null)
                {
                    return new Guid(Request.QueryString["ComplainID"]);
                }
                return Guid.Empty;
            }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Initialize();
            }
        }

        private void Initialize()
        {
            BizOrderComplaintBll bll = new BizOrderComplaintBll();
            BizOrderComplaintEntity entity = bll.GetOrderComplaintEntity(ComplainID);

            if (entity != null)
            {
                ltlComplaintContent.Text = entity.Content;
                ltlComplainter.Text = entity.CustomerName +" "+ entity.CreatedAt;

                if (entity.Status != EnumDescAttribute.GetFieldText(EnumHelper.OrderComplaintStatus.Processing))
                {
                    pnlReply.Visible = false;
                }
            }

            BindReply();
        }

        private void BindReply()
        {
            BizOrderComplaintBll bll = new BizOrderComplaintBll();
            List<BizOrderComplaintReplyEntity> list = bll.GetAll<BizOrderComplaintReplyEntity>(" and ComplainID='" + ComplainID + "'", " order by replyat ");
            rptReply.DataSource = list;
            rptReply.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            BizOrderComplaintReplyEntity entity = new BizOrderComplaintReplyEntity();
            entity.ComplainID = ComplainID;
            entity.UserType = "Customer";
            entity.ReplyBy = base.User.Identity.LoginUser.UserID;
            entity.ReplyByName = base.User.Identity.LoginUser.UserName;
            entity.ReplyAt = DateTime.Now;
            entity.Deleted = false;
            entity.Content = txtContent.Text;
            BizOrderComplaintBll bll = new BizOrderComplaintBll();
            bll.Insert(entity);

            BindReply();

            txtContent.Text = string.Empty;
        }
    }
}